package LearnAlgorithm.h_标准数学公式;

/*
int[] arr = new int[] {x^0, x^1, x^2, x^3, ..., x^n}//x != 1
特殊当|x| < 1, Sn是无穷递减的
 */
public class d几何级数 {
	public static void main(String[] args) {
		int[] arr1 = new int[] {1,2,4,8,16,32,64};
		int[] arr2 = new int[] {1,3,9,27,81,243,729};
		double[] arr3 = new double[] {1,0.5,0.25,0.125,0.0625};
		System.out.println(GeometricProgression(arr1));
		System.out.println(GeometricProgression(arr2));
		System.out.println(GeometricProgression(arr3));
		System.out.println(GeometricProgressionInfinite(arr3));
	}
	
	/**
	 * @param arr
	 * @return
	 */
	public static int GeometricProgression(int[] arr) {
		int x = arr[1];
		int n = arr.length - 1;//自己去画图就明白为什么减1
		return (int) ((Math.pow(x, n + 1) - 1) / (x - 1));
	}
	
	/**
	 * @param arr
	 * @return
	 */
	public static double GeometricProgression(double[] arr) {
		double x = arr[1];
		double n = arr.length - 1;//自己去画图就明白为什么减1
		return ((Math.pow(x, n + 1) - 1) / (x - 1));
	}
	
	/**
	 * |x| < 1
	 * 求极限
	 * @param arr
	 * @return
	 */
	public static double GeometricProgressionInfinite(double[] arr) {
		double x = arr[1];
		return 1 / (1 - x);
	}
}
